package com.ly.controller;


import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ly.bean.Bis_coupon_active;
import com.ly.bean.Bis_coupon_record;
import com.ly.service.CouponActiveService;
import com.ly.util.Tools;
import com.xbw.bean.Sys_User;

@Controller
public class CouponActiveHandler {
	@Autowired
	private CouponActiveService service;
	/**
	 * 查询礼券活动信息表
	 * @param active
	 * @param map
	 * @param page
	 * @return
	 */
	@RequestMapping("querycouponactive")
	public String querycouponactive(Bis_coupon_active active,Map<String, Object> map,@RequestParam(value="page",defaultValue="1")int page,Model model){
		System.out.println("name----------"+active.getActive_name()+",-----------"+active.getIs_del());
		PageHelper.startPage(page, 5);
		List<Map> list=service.querycouponactive(active);
		PageInfo<Map> info=new PageInfo<>(list);
		map.put("info", info);
		map.put("list", list);
		map.put("active", active);
		return "ly/showcouponrecord";
	}
	/**
	 * 修改礼券活动信息状态并修改礼券兑换记录状态
	 * @param active_id
	 * @return
	 */
	@RequestMapping("updateactive/{active_id}")
	public String updateactive(@PathVariable("active_id")Integer active_id){
		System.out.println(active_id);
		service.updatestatus(active_id);
		service.updaterecord(active_id);
		return "redirect:/querycouponactive";
	}
	/**
	 * 添加礼券活动信息并添加礼券兑换记录
	 * @param active
	 * @param record
	 * @param session
	 * @return
	 */
	@RequestMapping("insertactive")
	public String insertactive(Bis_coupon_active active,Bis_coupon_record record,HttpSession session){
		Sys_User user=(Sys_User) session.getAttribute("sysuser");
		active.setCreate_user_id(user.getUser_id());
		active.setModify_user_id(user.getUser_id());
		service.insert(active);
		Integer maxid=service.querymaxid();
		System.out.println(active.getBeauty_num());
		System.out.println(maxid);
		Set<String> cdKeySet=Tools.createCdkey(active.getBeauty_num(), 10);
		Iterator<String> it=cdKeySet.iterator();
		while (it.hasNext()) {
			record.setActive_id(maxid);
			record.setCdkey(it.next());
			service.insertrecord(record);
		}
		return "redirect:/querycouponactive";
	}
}
